$(function() {

	var sp = new Vue({
		el: "#wrapper",
		data: {
			gjTime: "",
			jg: {},
			gaoj: {},
			zd: {},
			worklist: [],
			timeDiffArray: [],
			workState: "",
			workListInfo: {},
			worksArray: [],
			status: "",
			remark: "",
			alarmReason: ""
		},

		methods: {
			showType: function(param) {
				if(param == '1') {
					return "一般告警";
				} else {
					return "严重告警";

				}
			},
			//	型号规格(1圆形，2方形，3外方内圆，4其他)
			showModel: function(param) {

				if(param == '1') {
					return "圆形";
				} else if(param == '2') {
					return "方形";
				} else if(param == '3') {
					return "外方内圆";
				} else {
					return "其他";

				}
			},
			showMetal: function(param) {
				//1	复合材料	RES_MATERIAL	材质	1	修改 删除 添加键值
				//2	铸铁	RES_MATERIAL	材质	2	修改 删除 添加键值
				//3	水泥	RES_MATERIAL	材质	3	修改 删除 添加键值
				//4	其他

				function getCZText(p_text) {
					if(p_text == '1') {
						return "复合材料";
					} else if(p_text == '2') {
						return "铸铁";
					} else if(p_text == '3') {
						return "水泥";
					} else {
						return "其他";
					}
				}

				if(param == null || param == '') {
					return "";
				}
				var czArray = param.split(",");
				var czString = "";
				for(var i = 0; i < czArray.length; i++) {
					var cell = czArray[i];
					czString += " ";
					czString += getCZText(cell);
				}
				return czString;

			},

			generateWork: function(pid) {
				console.log("告警pid---------" + pid);
				//					return;
				generate(pid);
			},
			chuangan: function(pid) {
				//				alert(pid);
				localStorage.setItem("eventId", pid);
				localStorage.setItem("type", "");

				hui.open("sensorFromDB.html?time" + apiManage.getNow(), {}, true, {});

			},

			geshi: function(param) {
				return formatTime(param);
			},
			back: function() {
				hui.back();

			},
			worklist: function() {
				hui.open("work_list.html", {}, true, {});
			}

		}
	});

	function showStatus(param) {
		var text = "";
		if(param == '10') {
			text = '新建';
		} else if(param == '20') {
			text = '待指派';
		} else if(param == '30') {
			text = '待接收';

		} else if(param == '40') {
			text = '待处理';

		} else if(param == '50') {
			text = '进一步处理';

		} else if(param == '60') {
			text = '进已完成';
		} else {
			text = '已取消';
		}

		return text;
	}

	//生成工单
	function generate(gaoji_id) {

		//				var zdCode = localStorage.getItem("zdCode");
		var path = urlManager.generateWork() + "?id=" + gaoji_id;
		var username = apiManage.getUsername();
		var sec = apiManage.getSecPwd();
		var time = apiManage.getNow();
		var key = "loginName=" + username + "&passWord=" + sec + "&time=" + time;
		var paramKey = hex_md5(key);

		hui.loading("工单生成中");
		$.ajax({
			type: "GET",
			url: path,
			beforeSend: function(request) {
				request.setRequestHeader("loginName", username);
				request.setRequestHeader("time", time);
				request.setRequestHeader("sign", paramKey);
				request.setRequestHeader("Content-Type", "application/text");

			},
			success: function(result) {
				//                  alert("生成工单成功!");
				hui.toast("成功生成工单");
				//						swal();
				//						swal("提示", "生成工单成功", "info");

				console.log("生成工单:" + JSON.stringify(result));
				hui.open("alarm_list.html?time" + apiManage.getNow(), {}, true, {});

			}

		});

	}

	loadData();
	//加载告警详情信息
	function loadData() {
		hui.loading("");

		var zdCode = localStorage.getItem("zdCode");
		var eventId = localStorage.getItem("eventId");

		var path = urlManager.alermDetail() + "?zdcode=" + zdCode + "&eventId=" + eventId;

		console.log("path====================" + path);
		var username = apiManage.getUsername();
		var sec = apiManage.getSecPwd();
		var time = apiManage.getNow();
		var key = "loginName=" + username + "&passWord=" + sec + "&time=" + time;
		var paramKey = hex_md5(key);

		$.ajax({
			type: "GET",
			url: path,
			beforeSend: function(request) {
				request.setRequestHeader("loginName", username);
				request.setRequestHeader("time", time);
				request.setRequestHeader("sign", paramKey);
				request.setRequestHeader("Content-Type", "application/text");

			},
			success: function(result) {

				hui.closeLoading();
				console.log("告警详情:" + JSON.stringify(result));

				if(result.workProcessList == null || result.workProcessList.length == 0) {
					$(".work_record").hide();
				} else {
					sp.worklist = result.workProcessList;
					readyData();
				}

				sp.jg = result.resJg;
				sp.gaoj = result.alarmInfo;
				//										gaoj.lastoccurtime=jg.installTime;
				sp.gjTime = result.alarmInfo.lastoccurtime;

			}

		});

	}

	//准备数据
	function readyData() {
		var datas = sp.worklist;
		var worksArray = new Array(); //装入列表
		var workListInfo = new Object(); //按状态对应时间

		if(datas == null || datas.length == 0) {
			return;
		}
		for(var i = 0; i < datas.length; i++) {
			var cell = datas[i];
			if(cell.thisState == '10') { //工单
				workListInfo.gd = cell;
				worksArray.push(cell);
			} else if(cell.thisState == '30') { //待接收
				workListInfo.djs = cell;
				worksArray.push(cell);
			} else if(cell.thisState == '40') { //处理
				workListInfo.cl = cell;
				worksArray.push(cell);
			} else if(cell.thisState == '60') { //完成
				workListInfo.wc = cell;
				worksArray.push(cell);
			} else {}
		}
		sp.workListInfo = workListInfo;

		sp.worksArray = worksArray;

		//	        	      console.log("workListInfo "+JSON.stringify(workListInfo));

		//	      console.log("worksArray "+JSON.stringify(worksArray));

		settingHtml();
		readyTimeDiff();

	}

	//时间差数组
	function readyTimeDiff() {
		var gjTime = sp.gjTime;

		var gdTime = "";
		var djsTime = "";
		var clTime = "";
		var wcTime = "";

		console.log("sp.workListInfo." + JSON.stringify(sp.workListInfo));
		if(sp.workListInfo.gd != null && sp.gjTime != null) {
			var time = timeDiff(sp.gjTime, formatTime(sp.workListInfo.gd.workTime));
			gdTime = time;
		}

		if(sp.workListInfo.djs != null) {
			var time = timeDiff(formatTime(sp.workListInfo.gd.workTime), formatTime(sp.workListInfo.djs.workTime));
			djsTime = time;

		}
		if(sp.workListInfo.cl != null) {
			var time = timeDiff(formatTime(sp.workListInfo.djs.workTime), formatTime(sp.workListInfo.cl.workTime));
			clTime = time;

		}
		if(sp.workListInfo.wc != null) {
			var time = timeDiff(formatTime(sp.workListInfo.cl.workTime), formatTime(sp.workListInfo.wc.workTime));
			wcTime = time;

		}
		var readyArray = new Array();
		// sp.timeDiffArray.push(gjTime);
		readyArray.push(gdTime);
		readyArray.push(djsTime);
		readyArray.push(clTime);
		readyArray.push(wcTime);

		// 	       console.log("hahahahha"+JSON.stringify(readyArray));

		sp.timeDiffArray = readyArray;

		var html = "";
		for(var i = 0; i < sp.timeDiffArray.length; i++) {
			html += "<em>" + sp.timeDiffArray[i] + "</em>";
		}
		// 	      return html;
		$(".record_time").html(html);
		console.log("--------------------" + html);
	}

	function timeDiff(startTime, endTime) {
		console.log("两个时间之间相差多久 并格式化--" + startTime + "  " + endTime);

		startTime = startTime.replace(/\-/g, "/");
		endTime = endTime.replace(/\-/g, "/");

		var diffDate = new Date(endTime).getTime() - new Date(startTime).getTime();
		console.log("相差--" + diffDate);

		var days = Math.floor(diffDate / (24 * 3600 * 1000));
		var leave1 = diffDate % (24 * 3600 * 1000);
		var hours = Math.floor(leave1 / (3600 * 1000));
		var leave2 = leave1 % (3600 * 1000);
		var minutes = Math.floor(leave2 / (60 * 1000));
		var leave3 = leave2 % (60 * 1000);
		var seconds = Math.round(leave3 / 1000);
		//        console.log(days + " 天 " + hours + "时 " + minutes + " 分 " + seconds + "秒");

		var timeString = "";
		if(days > 0) {
			timeString += days + "天";
		}
		if(hours > 0) {
			timeString += hours + "小时";
		}
		timeString += minutes + "分钟";

		console.log(timeString);

		return timeString;
	}

	//根据数据返回流程视图样式
	function settingHtml() {
		var workListInfo = sp.workListInfo;

		//			console.log("workListInfo                            "+JSON.stringify(workListInfo));
		var worksArray = sp.worksArray;
		var gdTime = "";
		if(workListInfo.gd != null) {
			gdTime = formatTime(workListInfo.gd.workTime);

		}
		var djsTime = "";
		if(workListInfo.djs) {
			djsTime = formatTime(workListInfo.djs.workTime);

		}
		var clTime = "";
		if(workListInfo.cl) {
			clTime = formatTime(workListInfo.cl.workTime);
		}
		var wcTime = "";
		if(workListInfo.wc) {
			wcTime = formatTime(workListInfo.wc.workTime);
		}

		var length = worksArray.length;
		var html = "";

		var gjTime = sp.gjTime;
		var styleType = "";
		switch(length) {

			case 0:
				{
					html =
					"  <p>" +
					"      <em class=\"green\">&nbsp;</em>" +
					"      <b>告警</b>" +
					"      <i>" + gjTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em  class=\"white\">&nbsp;</em>" +
					"      <b>工单</b>" +
					"      <i>" + gdTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>待接收</b>" +
					"      <i>" + djsTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>处理</b>" +
					"      <i>" + clTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>完成</b>" +
					"      <i>" + wcTime + "</i>" +
					"    </p>";

				}
				break;

			case 1:
				{
					html =
					"  <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>告警</b>" +
					"      <i>" + gjTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"green\">&nbsp;</em>" +
					"      <b>工单</b>" +
					"      <i>" + gdTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>待接收</b>" +
					"      <i>" + djsTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>处理</b>" +
					"      <i>" + clTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>完成</b>" +
					"      <i>" + wcTime + "</i>" +
					"    </p>";

				}
				break;

			case 2:
				{
					html =
					"  <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>告警</b>" +
					"      <i>" + gjTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>工单</b>" +
					"      <i>" + gdTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"green\">&nbsp;</em>" +
					"      <b>待接收</b>" +
					"      <i>" + djsTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>处理</b>" +
					"      <i>" + clTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>完成</b>" +
					"      <i>" + wcTime + "</i>" +
					"    </p>";
				}
				break;
			case 3:
				{
					html =
					"  <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>告警</b>" +
					"      <i>" + gjTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>工单</b>" +
					"      <i>" + gdTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>待接收</b>" +
					"      <i>" + djsTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"green\">&nbsp;</em>" +
					"      <b>处理</b>" +
					"      <i>" + clTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"white\">&nbsp;</em>" +
					"      <b>完成</b>" +
					"      <i>" + wcTime + "</i>" +
					"    </p>";
				}
				break;
			case 4:
				{
					html =
					"  <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>告警</b>" +
					"      <i>" + gjTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>工单</b>" +
					"      <i>" + gdTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>待接收</b>" +
					"      <i>" + djsTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em>&nbsp;</em>" +
					"      <b>处理</b>" +
					"      <i>" + clTime + "</i>" +
					"    </p>" +
					"    <p>" +
					"      <em class=\"green\">&nbsp;</em>" +
					"      <b>完成</b>" +
					"      <i>" + wcTime + "</i>" +
					"    </p>";
				}
				break;

			default:
		}

		//			return html;
		$(".record_step").html(html);
	}

	Date.prototype.format = function(format) //author: meizz
	{
		var o = {
			"M+": this.getMonth() + 1, //month
			"d+": this.getDate(), //day
			"h+": this.getHours(), //hour
			"m+": this.getMinutes(), //minute
			"s+": this.getSeconds(), //second
			"q+": Math.floor((this.getMonth() + 3) / 3), //quarter
			"S": this.getMilliseconds() //millisecond
		}
		if(/(y+)/.test(format)) format = format.replace(RegExp.$1,
			(this.getFullYear() + "").substr(4 - RegExp.$1.length));
		for(var k in o)
			if(new RegExp("(" + k + ")").test(format))
				format = format.replace(RegExp.$1,
					RegExp.$1.length == 1 ? o[k] :
					("00" + o[k]).substr(("" + o[k]).length));
		return format;
	}

	function formatTime(val) {
		if(val == null || val == '') {
			return "";
		}
		var re = /-?\d+/;
		var m = re.exec(val);
		//	         m = m.replace(/\-/g, "/");
		var d = new Date(parseInt(m[0]));

		// 按【2012-02-13 09:09:09】的格式返回日期
		//	        var tmp  = d.format("yyyy-MM-dd hh:mm:ss");
		//	        console.log("格式化时间:   "+tmp);
		return d.format("yyyy-MM-dd hh:mm:ss");
	}
});